package com.xajh.duoduo.environment;

import com.alibaba.fastjson.JSON;
import com.xajh.duoduo.domain.PositionDomain;
import com.xajh.duoduo.eunm.position.JoinGameHome;
import com.xajh.duoduo.eunm.position.JoinGameRole;
import com.xajh.duoduo.interfac.Holder;
import org.apache.logging.log4j.core.util.FileUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Component;
import org.springframework.util.ResourceUtils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Field;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

@Component
public class PositionHolder implements Holder, InitializingBean {
    private List<PositionDomain> mainTaskPositions;
    private List<PositionDomain> answerQuestionPositions;
//    private List<PositionDomain> joinGameHomes;
//    private List<PositionDomain> joinGameRoles;
//    private List<PositionDomain> menuPositions;
//    private List<PositionDomain> rolePositions;
//    private List<PositionDomain> systemPositions;


    public static void main(String[] args) {
        List<PositionDomain> list = new ArrayList<>();
        for(JoinGameRole item: JoinGameRole.values()){
            com.xajh.duoduo.domain.AnswerQuestionPosition position = new com.xajh.duoduo.domain.AnswerQuestionPosition();
            position.setX(item.getX());
            position.setY(item.getY());
            position.setComment(item.getComment());
            list.add(position);
        }

        System.out.println(JSON.toJSONString(list));
    }

    @Override
    public boolean getTargetResource() throws Exception {
//        Field[] fields = this.getClass().getFields();
        Field[] fields = this.getClass().getDeclaredFields();
        for (int i = 0 ; i < fields.length ; i++){
            fields[i].setAccessible(true);
            fields[i].set(this,this.init(fields[i].getName()));
        }
        return false;
    }

    private List<PositionDomain> init(String fieldName) throws Exception{
        ClassPathResource classPathResource = new ClassPathResource(fieldName + ".json");
//        File file = new ClassPathResource(fieldName + ".json").getFile();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(classPathResource.getInputStream()));
        StringBuilder sb = new StringBuilder();
        String str;
        while ((str = bufferedReader.readLine()) != null){
            sb.append(str);
        }

        return JSON.parseArray(sb.toString(),PositionDomain.class);

    }


    @Override
    public void afterPropertiesSet() throws Exception {
        this.getTargetResource();
    }

    public List<PositionDomain> getMainTaskPositions() {
        return mainTaskPositions;
    }

    public void setMainTaskPositions(List<PositionDomain> mainTaskPositions) {
        this.mainTaskPositions = mainTaskPositions;
    }

    public List<PositionDomain> getAnswerQuestionPositions() {
        return answerQuestionPositions;
    }

    public void setAnswerQuestionPositions(List<PositionDomain> answerQuestionPositions) {
        this.answerQuestionPositions = answerQuestionPositions;
    }

//    public List<PositionDomain> getJoinGameHomes() {
//        return joinGameHomes;
//    }
//
//    public void setJoinGameHomes(List<PositionDomain> joinGameHomes) {
//        this.joinGameHomes = joinGameHomes;
//    }
//
//    public List<PositionDomain> getJoinGameRoles() {
//        return joinGameRoles;
//    }
//
//    public void setJoinGameRoles(List<PositionDomain> joinGameRoles) {
//        this.joinGameRoles = joinGameRoles;
//    }
//
//    public List<PositionDomain> getMenuPositions() {
//        return menuPositions;
//    }
//
//    public void setMenuPositions(List<PositionDomain> menuPositions) {
//        this.menuPositions = menuPositions;
//    }
//
//    public List<PositionDomain> getRolePositions() {
//        return rolePositions;
//    }
//
//    public void setRolePositions(List<PositionDomain> rolePositions) {
//        this.rolePositions = rolePositions;
//    }
//
//    public List<PositionDomain> getSystemPositions() {
//        return systemPositions;
//    }
//
//    public void setSystemPositions(List<PositionDomain> systemPositions) {
//        this.systemPositions = systemPositions;
//    }
}
